﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;



namespace WindowsFormsApplication1.CardConfig
{
    public partial class FormCard : Form
    {
        private bool change = false;
        public FormCard()
        {
            InitializeComponent();
        }

        private void FormCard_Load(object sender, EventArgs e)
        {
            try
            {
                Monitor_DB.instance.Refresh_DB();
                this.tb_cardBindingSource.DataSource = Monitor_DB.instance.db.tb_card;
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tb_cardBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
        	DataTable daorig = new DataTable();
            DataTable dadest = new DataTable();
            
            int[] check = new int[4] { 0, 0, 0, 1 };
            
            try
            {
               SqlConnection sqlCon = new SqlConnection(UserLog.connectionstring);
               SqlDataAdapter sda = new SqlDataAdapter();
               SqlDataAdapter sda2 = new SqlDataAdapter();

 
               string strSql = "select * from tb_card";
               SqlCommand cmd = new SqlCommand(strSql, sqlCon);
               sda.SelectCommand = cmd;
               sda.Fill(daorig);

               

               tb_cardDataGridView.EndEdit();

                Monitor_DB.instance.db.SubmitChanges();

                change = false;

                sda.Fill(dadest);
                       
                UserLog.addrecordfordatagridview(daorig, dadest, UserLog.formcard, "配置门卡", "成功", check);

                foreach (DataRow orig in daorig.Rows)
                {
                    Console.WriteLine(orig[0].ToString() + orig[1].ToString() + orig[2].ToString() + orig[3].ToString());
                }

                MessageBox.Show("保存成功！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
     

            }
            catch (Exception Ex)
            {
                UserLog.addrecordfordatagridview(daorig, dadest, UserLog.formcard, "配置门卡", "失败", check);
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Monitor_DB.instance.Refresh_DB();
            }
        }

        private void tb_cardDataGridView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control == true && e.KeyCode == Keys.S)
            {
                tb_cardBindingNavigatorSaveItem_Click(sender, e);
            }
        }

        private void FormCard_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (change)
            {
                DialogResult r = MessageBox.Show("保存修改？", "关闭", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (r == DialogResult.Yes)
                {
                    tb_cardBindingNavigatorSaveItem_Click(sender, e);
                }
                else if (r == DialogResult.Cancel)
                {
                    e.Cancel = true;

                }
            }
        }

        private void tb_cardDataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            change = true;
        }

        private void card_IdTextBox_Click(object sender, EventArgs e)
        {
            change = true;
        }

        private void inUseCheckBox_Click(object sender, EventArgs e)
        {
            change = true;
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {

        }

    }
}
